﻿using UnityEngine;
using System;

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Collections;


public class stg2_camera : MonoBehaviour {

    //키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트

    public List<KinectGestures.Gestures> userGestures1;
    private List<KinectGestures.GestureData> userGestures = new List<KinectGestures.GestureData>();

    public GUITexture backgroundImage;
    public KinectWrapper.NuiSkeletonPositionIndex TrackedJoint = KinectWrapper.NuiSkeletonPositionIndex.HandRight;
    public GameObject OverlayObject;
    public float smoothFactor = 100f;

    public GUIText debugText;
    private float distanceToCamera = 10f;
    public static float cur_x, cur_y, cur_z;
    float touch_z = 0, touch_y = 0;
    private bool progressDisplayed = false;
    public Vector2 onGui_pos = new Vector2(0, 0);
    //키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트키넥트


	// Use this for initialization
	void Start () {
        if (OverlayObject)
        {
            distanceToCamera = (OverlayObject.transform.position - Camera.main.transform.position).magnitude;
        }
	}
	
	// Update is called once per frame
	void Update () {
        KinectManager manager = KinectManager.Instance;

        if (manager && manager.IsInitialized())
        {
            //backgroundImage.renderer.material.mainTexture = manager.GetUsersClrTex();
            if (backgroundImage && (backgroundImage.texture == null))
            {
                backgroundImage.texture = manager.GetUsersClrTex();
            }

            int iJointIndex = (int)TrackedJoint;

            if (manager.IsUserDetected())
            {
                uint userId = manager.GetPlayer1ID();

                if (manager.IsJointTracked(userId, iJointIndex))
                {
                    Vector3 posJoint = manager.GetRawSkeletonJointPos(userId, iJointIndex);

                    if (posJoint != Vector3.zero)
                    {


                        // 3d position to depth
                        Vector3 posDepth = manager.GetDepthMapPosForJointPos(posJoint);

                        // depth pos to color pos
                        Vector2 posColor = manager.GetColorMapPosForDepthPos(posDepth);
                        cur_x = (float)posColor.x / KinectWrapper.Constants.ColorImageWidth;
                        cur_y = 1.0f - (float)posColor.y / KinectWrapper.Constants.ColorImageHeight;
                        cur_z = posDepth.z;
                        Vector3 mouse = Input.mousePosition;
                        Vector3 mousePos = camera.ViewportToScreenPoint(new Vector3(cur_x, cur_y, cur_z));

                        if (debugText)
                        {
                            //debugText.guiText.text = "Tracked user ID: " + userId;  // new Vector2(scaleX, scaleY).ToString();
                            //debugText.guiText.text = "coordinate: " + new Vector2(scaleX, scaleY).ToString();
                            //debugText.guiText.text = "coordinate: " + cur_x.ToString() + ',' + cur_y.ToString() + ',' + posDepth.z.ToString();
                            debugText.guiText.text = "mouse: " + mouse.x.ToString() + ',' + mouse.y.ToString();
                        }

                        if (OverlayObject)
                        {
                            Vector3 vPosOverlay = Camera.main.ViewportToWorldPoint(new Vector3(cur_x, cur_y, distanceToCamera));
                            OverlayObject.transform.position = Vector3.Lerp(OverlayObject.transform.position, vPosOverlay, smoothFactor * Time.deltaTime);
                            MouseControl.MouseMove(new Vector3(cur_x,cur_y,0));
                        }
                    }
                }

            }

        }
	}
}
